using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
namespace Microsoft.Phone.Controls
{
	[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(MultiselectList))]
	public class MultiselectList : ItemsControl
	{
		public static readonly DependencyProperty IsInSelectionModeProperty = DependencyProperty.Register("IsSelectionEnabled", typeof(bool), typeof(MultiselectList), new PropertyMetadata(false, new PropertyChangedCallback(MultiselectList.OnIsSelectionEnabledPropertyChanged)));
		public static readonly DependencyProperty ItemInfoTemplateProperty = DependencyProperty.Register("ItemInfoTemplate", typeof(DataTemplate), typeof(MultiselectList), new PropertyMetadata(null, null));
		public static readonly DependencyProperty ItemContainerStyleProperty = DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(MultiselectList), new PropertyMetadata(null, null));
		public event SelectionChangedEventHandler SelectionChanged;
		public event DependencyPropertyChangedEventHandler IsSelectionEnabledChanged;
		public IList SelectedItems
		{
			get;
			private set;
		}
		public bool IsSelectionEnabled
		{
			get
			{
				return (bool)base.GetValue(MultiselectList.IsInSelectionModeProperty);
			}
			set
			{
				base.SetValue(MultiselectList.IsInSelectionModeProperty, value);
			}
		}
		public DataTemplate ItemInfoTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(MultiselectList.ItemInfoTemplateProperty);
			}
			set
			{
				base.SetValue(MultiselectList.ItemInfoTemplateProperty, value);
			}
		}
		public Style ItemContainerStyle
		{
			get
			{
				return (Style)base.GetValue(MultiselectList.ItemContainerStyleProperty);
			}
			set
			{
				base.SetValue(MultiselectList.ItemContainerStyleProperty, value);
			}
		}
		private static void OnIsSelectionEnabledPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			MultiselectList multiselectList = (MultiselectList)obj;
			if ((bool)e.get_NewValue())
			{
				multiselectList.OpenSelection();
			}
			else
			{
				if (multiselectList.SelectedItems.get_Count() > 0)
				{
					IList list = new List<object>();
					IEnumerator enumerator = multiselectList.SelectedItems.GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							object current = enumerator.get_Current();
							list.Add(current);
						}
					}
					finally
					{
						IDisposable disposable = enumerator as IDisposable;
						if (disposable != null)
						{
							disposable.Dispose();
						}
					}
					int num = 0;
					while (num < multiselectList.get_Items().get_Count() && multiselectList.SelectedItems.get_Count() > 0)
					{
						MultiselectItem multiselectItem = (MultiselectItem)multiselectList.get_ItemContainerGenerator().ContainerFromIndex(num);
						if (multiselectItem != null && multiselectItem.IsSelected)
						{
							multiselectItem._canTriggerSelectionChanged = false;
							multiselectItem.IsSelected = false;
							multiselectItem._canTriggerSelectionChanged = true;
						}
						num++;
					}
					multiselectList.SelectedItems.Clear();
					multiselectList.OnSelectionChanged(list, new object[0]);
				}
				multiselectList.CloseSelection();
			}
			DependencyPropertyChangedEventHandler isSelectionEnabledChanged = multiselectList.IsSelectionEnabledChanged;
			if (isSelectionEnabledChanged != null)
			{
				isSelectionEnabledChanged.Invoke(obj, e);
			}
		}
		public MultiselectList()
		{
			base.set_DefaultStyleKey(typeof(MultiselectList));
			this.SelectedItems = new List<object>();
		}
		internal void OnSelectionChanged(IList removedItems, IList addedItems)
		{
			if (this.SelectedItems.get_Count() <= 0)
			{
				this.IsSelectionEnabled = false;
			}
			else
			{
				if (this.SelectedItems.get_Count() == 1 && removedItems.get_Count() == 0)
				{
					this.IsSelectionEnabled = true;
				}
			}
			SelectionChangedEventHandler selectionChanged = this.SelectionChanged;
			if (selectionChanged != null)
			{
				selectionChanged.Invoke(this, new SelectionChangedEventArgs(removedItems, addedItems));
			}
		}
		private void OpenSelection()
		{
			IList<WeakReference> itemsInViewPort = ItemsControlExtensions.GetItemsInViewPort(this);
			using (IEnumerator<WeakReference> enumerator = itemsInViewPort.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					WeakReference current = enumerator.get_Current();
					MultiselectItem multiselectItem = (MultiselectItem)current.get_Target();
					multiselectItem.State = SelectionEnabledState.Opened;
					multiselectItem.UpdateVisualState(true);
				}
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				for (int i = 0; i < base.get_Items().get_Count(); i++)
				{
					MultiselectItem multiselectItem2 = (MultiselectItem)base.get_ItemContainerGenerator().ContainerFromIndex(i);
					if (multiselectItem2 != null)
					{
						multiselectItem2.State = SelectionEnabledState.Opened;
						multiselectItem2.UpdateVisualState(false);
					}
				}
			});
		}
		private void CloseSelection()
		{
			IList<WeakReference> itemsInViewPort = ItemsControlExtensions.GetItemsInViewPort(this);
			using (IEnumerator<WeakReference> enumerator = itemsInViewPort.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					WeakReference current = enumerator.get_Current();
					MultiselectItem multiselectItem = (MultiselectItem)current.get_Target();
					multiselectItem.State = SelectionEnabledState.Closed;
					multiselectItem.UpdateVisualState(true);
				}
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				for (int i = 0; i < base.get_Items().get_Count(); i++)
				{
					MultiselectItem multiselectItem2 = (MultiselectItem)base.get_ItemContainerGenerator().ContainerFromIndex(i);
					if (multiselectItem2 != null)
					{
						multiselectItem2.State = SelectionEnabledState.Closed;
						multiselectItem2.UpdateVisualState(false);
					}
				}
			});
		}
		protected override DependencyObject GetContainerForItemOverride()
		{
			return new MultiselectItem();
		}
		protected override bool IsItemItsOwnContainerOverride(object item)
		{
			return item is MultiselectItem;
		}
		protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
		{
			base.PrepareContainerForItemOverride(element, item);
			MultiselectItem multiselectItem = (MultiselectItem)element;
			multiselectItem.set_Style(this.ItemContainerStyle);
			multiselectItem._isBeingVirtualized = true;
			multiselectItem.IsSelected = this.SelectedItems.Contains(item);
			multiselectItem.State = (this.IsSelectionEnabled ? SelectionEnabledState.Opened : SelectionEnabledState.Closed);
			multiselectItem.UpdateVisualState(false);
			multiselectItem._isBeingVirtualized = false;
		}
		protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
		{
			base.OnItemsChanged(e);
			if (this.SelectedItems.get_Count() > 0)
			{
				IList list = new List<object>();
				for (int i = 0; i < this.SelectedItems.get_Count(); i++)
				{
					object obj = this.SelectedItems.get_Item(i);
					if (!base.get_Items().Contains(obj))
					{
						this.SelectedItems.Remove(obj);
						list.Add(obj);
						i--;
					}
				}
				this.OnSelectionChanged(list, new object[0]);
			}
		}
	}
}
